package com.project.easy.common.framework.web.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.project.easy.common.framework.web.service.IFrameworkDicService;
import  com.project.easy.common.util.collection.CollectionUtil;
import  com.project.easy.common.util.constants.CommonConstant;
import  com.project.easy.common.util.exception.business.BusinessException;
import  com.project.easy.common.util.string.StringUtil;
import  com.project.easy.common.util.tree.TreeUtil;
import com.project.easy.common.framework.web.entity.FrameworkDic;
import com.project.easy.common.framework.web.entity.dto.DicAddReq;
import com.project.easy.common.framework.web.entity.dto.DicTreeNodeRes;
import com.project.easy.common.framework.web.entity.dto.DicUpdateReq;
import com.project.easy.common.framework.web.entity.dto.DicVerifyCodeReq;
import com.project.easy.common.framework.web.mapper.FrameworkDicMapper;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 * 字典表 服务实现类
 * </p>
 *
 * @author GreatorMan.cool
 * @since 2022-07-20
 */
@Service
public class FrameworkDicServiceImpl extends ServiceImpl<FrameworkDicMapper, FrameworkDic> implements IFrameworkDicService {
    /**
     * 新增字典
     *
     * @param param 参数
     * @return 反回ID
     */
    @Override
    public Long add(DicAddReq param) {
        Boolean aBoolean = verifyCode(new DicVerifyCodeReq(param.getDicCode(), null, param.getParentId()));
        if (aBoolean) {
            throw  new BusinessException("字段编码重复不能新增!");
        }
        String sysCode = CommonConstant.DEFAULT_SYS_CODE;
        if (StringUtil.isNotBlank(param.getSysCode())) {
            sysCode = param.getSysCode();
        }
        FrameworkDic frameworkDic = new FrameworkDic();
        frameworkDic.setDicCode(param.getDicCode());
        frameworkDic.setSysCode(sysCode);
        frameworkDic.setParentId(param.getParentId());
        frameworkDic.setDicName(param.getDicName());
        this.baseMapper.insert(frameworkDic);
        return frameworkDic.getId();
    }
    /**
     * 迁移数据
     *
     * @param param 修改参数
     * @return true 成功 false 失败
     */
    @Override
    public Boolean updateObject(DicUpdateReq param) {
        if (Objects.isNull(param.getId())) {
            throw  new BusinessException("参数非法!");
        }
        FrameworkDic frameworkDic1 = this.baseMapper.selectById(param.getId());
        if (Objects.isNull(frameworkDic1)) {
            throw  new BusinessException("参数非法!");
        }
        Boolean aBoolean = verifyCode(new DicVerifyCodeReq(param.getDicCode(), frameworkDic1.getId(), frameworkDic1.getParentId()));
        if (aBoolean) {
            throw  new BusinessException("字段编码重复不能新增!");
        }
        frameworkDic1.setDicCode(param.getDicCode());
        frameworkDic1.setDicName(param.getDicName());
        this.baseMapper.updateById(frameworkDic1);
        return Boolean.TRUE;
    }

    /**
     * 验证编码是否重复
     *
     * @param param 参数
     * @return true 存在false  不存在
     */
    @Override
    public Boolean verifyCode(DicVerifyCodeReq param) {
        if (StringUtil.isBlank(param.getCode()) || Objects.isNull(param.getParentId())) {
            throw  new BusinessException("参数非法!");
        }
        QueryWrapper<FrameworkDic> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id", param.getParentId());
        queryWrapper.eq("has_del", Boolean.FALSE);
        queryWrapper.eq("status", Boolean.TRUE);
        queryWrapper.eq("sys_code", CommonConstant.DEFAULT_SYS_CODE);
        queryWrapper.eq("dic_code", param.getCode());
        FrameworkDic frameworkDics = this.baseMapper.selectOne(queryWrapper);
        if (Objects.nonNull(param.getId()) && Objects.nonNull(frameworkDics)) {
            if (frameworkDics.getId().compareTo(param.getId()) == 0) {
                return Boolean.FALSE;
            }
        }
        return  Objects.nonNull(frameworkDics);
    }

    /**
     * 字典管理
     *
     * @param parent 父级ID
     * @return 返回字典树
     */
    @Override
    public List<DicTreeNodeRes> tree(Long parent) {
        QueryWrapper<FrameworkDic> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("parent_id", parent);
        queryWrapper.eq("has_del", Boolean.FALSE);
        queryWrapper.eq("status", Boolean.TRUE);
        queryWrapper.eq("sys_code", CommonConstant.DEFAULT_SYS_CODE);
        List<FrameworkDic> frameworkDics = this.baseMapper.selectList(queryWrapper);
        List<DicTreeNodeRes> result = new ArrayList<>();
        if (!CollectionUtil.isNull(frameworkDics)) {
            for (FrameworkDic dic : frameworkDics) {
                DicTreeNodeRes dicTreeNodeRes = new DicTreeNodeRes();
                dicTreeNodeRes.setId(dic.getId());
                dicTreeNodeRes.setpId(dic.getParentId());
                dicTreeNodeRes.setName(dic.getDicName());
                dicTreeNodeRes.setCode(dic.getDicCode());
                result.add(dicTreeNodeRes);
            }
        }
        return TreeUtil.bulid(result, parent);
    }
    /**
     * 启用
     *
     * @param id ID
     * @return true 启用成功 false 启用失败
     */
    @Override
    public Boolean able(Long id) {
        if (Objects.isNull(id)) {
            throw new BusinessException("参数不能为空");
        }
        FrameworkDic frameworkDic = this.baseMapper.selectById(id);
        if (Objects.isNull(frameworkDic)) {
            throw  new BusinessException("参数非法!");
        }
        if (frameworkDic.getStatus()) {
            throw  new BusinessException("启用不能再次启用!");
        }
        frameworkDic.setStatus(Boolean.TRUE);
        this.baseMapper.updateById(frameworkDic);
        return Boolean.TRUE;
    }
    /**
     * 启用
     *
     * @param id ID
     * @return true 启用成功 false 启用失败
     */
    @Override
    public Boolean disAble(Long id) {
        if (Objects.isNull(id)) {
            throw new BusinessException("参数不能为空");
        }
        FrameworkDic frameworkDic = this.baseMapper.selectById(id);
        if (Objects.isNull(frameworkDic)) {
            throw  new BusinessException("参数非法!");
        }
        if (!frameworkDic.getStatus()) {
            throw  new BusinessException("禁用不能再次禁用!");
        }
        frameworkDic.setStatus(Boolean.FALSE);
        this.baseMapper.updateById(frameworkDic);
        return Boolean.TRUE;
    }
    /**
     * 格局父KEY 查询字典信息
     *
     * @return
     */
    @Override
    public List<Map<String, String>> queryByParentKey(String code) {
        LambdaQueryWrapper<FrameworkDic> frameworkDicLambdaQueryWrapper = new LambdaQueryWrapper<>();
        frameworkDicLambdaQueryWrapper.eq(FrameworkDic::getDicCode, code).eq(FrameworkDic::getParentId, 0)
                .eq(FrameworkDic::getStatus, 1).eq(FrameworkDic::getHasDel, 0);
        FrameworkDic frameworkDic = this.baseMapper.selectOne(frameworkDicLambdaQueryWrapper);
        if (Objects.isNull(frameworkDic)) {
            return CommonConstant.EMPTY_LIST;
        }
        LambdaQueryWrapper<FrameworkDic> frameworkDicLList = new LambdaQueryWrapper<>();
        frameworkDicLList.eq(FrameworkDic::getParentId, frameworkDic.getId())
                .eq(FrameworkDic::getStatus, Boolean.TRUE).eq(FrameworkDic::getHasDel, Boolean.FALSE);
        List<FrameworkDic> frameworkDics = list(frameworkDicLList);
        List<Map<String, String>> result = new ArrayList<>();
        if (CollectionUtil.isNotNull(frameworkDics)) {
            for (FrameworkDic dic: frameworkDics) {
                Map<String, String> data = new HashMap<>();
                data.put("key", dic.getDicCode());
                data.put("name", dic.getDicName());
                result.add(data);
            }
        }
        return  result;
    }
}
